
package com.tenentia.pcmps.swm.data.implementation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.data.DataAccessException;
import com.tenentia.pcmps.swm.data.beans.PDFTemplate;
import com.tenentia.pcmps.swm.data.interfaces.PDFTemplatesDAO;
import com.tenentia.pcmps.swm.data.util.IBatisUtilities;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class PDFTemplatesDAOImpl implements PDFTemplatesDAO {

	private static final Logger LOGGER = Logger.getLogger(PDFTemplatesDAOImpl.class.getPackage().getName());

	PDFTemplatesDAOImpl() {

		super();
	}

	@Override
	public void delete(PDFTemplate template) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		Exception catchedException = null;

		try {
			IBatisUtilities.getSqlMapClient().startTransaction();
			IBatisUtilities.getSqlMapClient().delete("deletePDFTemplate", template.getInternalID());
			IBatisUtilities.getSqlMapClient().commitTransaction();
		} catch (Exception e) {
			LOGGER.error(e, e);
			catchedException = e;
		} finally {
			try {
				IBatisUtilities.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				LOGGER.error(e, e);
			}
		}

		if (null != catchedException) {
			throw new DataAccessException(catchedException);
		}

	}

	@Override
	public PDFTemplate get(PDFTemplate template) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		PDFTemplate methodResult;

		try {
			methodResult = (PDFTemplate) IBatisUtilities.getSqlMapClient().queryForObject("getPDFTemplate", template);
			LOGGER.debug("methodResult: " + methodResult);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public PDFTemplate getPDFTemplateByAlias(String alias) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		PDFTemplate methodResult;

		try {
			PDFTemplate template = new PDFTemplate();

			template.setName(alias);

			methodResult = (PDFTemplate) IBatisUtilities.getSqlMapClient().queryForObject("getPDFTemplateByAlias", template);
			LOGGER.debug("methodResult: " + methodResult);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public PDFTemplate getPDFTemplateByClassNameAndDate(String className, Date classDate) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		PDFTemplate methodResult = null;

		try {

			PDFTemplate template = new PDFTemplate();

			template.setClassName(className);
			template.setDate(classDate);

			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getPDFTemplateByClassNameAndDate", template);

			if (null == tmpList || tmpList.size() == 0) {
				throw new Exception("No records found.");
			} else {
				methodResult = (PDFTemplate) tmpList.get(0);
				LOGGER.debug("methodResult: " + methodResult);
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	public List<PDFTemplate> getTableItemsByRange(Object filterParameters, Object sortParameters, Integer firstRow, Integer numberOfRows) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		LOGGER.debug("firstRow: " + firstRow.intValue());
		LOGGER.debug("numberOfRows: " + numberOfRows.intValue());

		List<PDFTemplate> methodResult;

		try {
			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getPDFTemplateItemsByRange", firstRow, numberOfRows);

			if (null == tmpList || tmpList.size() == 0) {
				LOGGER.debug("No results...");
				methodResult = new ArrayList<PDFTemplate>();
			} else {
				methodResult = new ArrayList<PDFTemplate>();
				for (int i = 0; i < tmpList.size(); i++) {
					PDFTemplate tmpInstance = (PDFTemplate) tmpList.get(i);
					methodResult.add(tmpInstance);
					LOGGER.debug("tmpInstance: " + tmpInstance);
				}
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	public Integer getTableRowCount(Object filterParameters) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		Integer methodResult;

		try {
			methodResult = (Integer) IBatisUtilities.getSqlMapClient().queryForObject("getPDFTemplateRowCount");
			LOGGER.debug("methodResult: " + methodResult.intValue());
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

	@Override
	public PDFTemplate save(PDFTemplate template) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		PDFTemplate methodResult = null;

		Exception catchedException = null;

		try {
			IBatisUtilities.getSqlMapClient().startTransaction();
			Long templateID = (Long) IBatisUtilities.getSqlMapClient().insert("insertPDFTemplate", template);
			template.setInternalID(templateID);
			IBatisUtilities.getSqlMapClient().update("updatePDFTemplateData", template);
			IBatisUtilities.getSqlMapClient().commitTransaction();
			methodResult = get(template);
		} catch (Exception e) {
			LOGGER.error(e, e);
			catchedException = e;
		} finally {
			try {
				IBatisUtilities.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				LOGGER.error(e, e);
			}
		}

		if (null != catchedException) {
			throw new DataAccessException(catchedException);
		}

		return methodResult;

	}

	@Override
	public PDFTemplate update(PDFTemplate template) throws DataAccessException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		PDFTemplate methodResult = null;

		Exception catchedException = null;

		try {
			IBatisUtilities.getSqlMapClient().startTransaction();
			IBatisUtilities.getSqlMapClient().update("updatePDFTemplate", template);
			IBatisUtilities.getSqlMapClient().update("updatePDFTemplateData", template);
			IBatisUtilities.getSqlMapClient().commitTransaction();
			methodResult = get(template);
		} catch (Exception e) {
			LOGGER.error(e, e);
			catchedException = e;
		} finally {
			try {
				IBatisUtilities.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				LOGGER.error(e, e);
			}
		}

		if (null != catchedException) {
			throw new DataAccessException(catchedException);
		}

		return methodResult;

	}

}
